package com.alibaba.citrus.cr.unified.inventory.repository.impl;

import com.alibaba.citrus.cr.unified.inventory.repository.AllocateRelationReadRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.domain.allocaterelation.dto.CheckExistResponse;
import com.epoch.app.crunifiedinventory.domain.allocaterelation.model.AllocateRelation;
import com.epoch.app.crunifiedinventory.domain.allocaterelation.service.AllocateRelationService;
import com.epoch.app.crunifiedinventory.dto.request.AllocateRelationLoadListRequest;
import com.epoch.app.crunifiedinventory.model.dto.AllocateRelationLoadListVO;
import com.epoch.app.crunifiedinventory.sdo.AllocateRelationSDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: 王成
 * @date:
 **/
@Component
public class AllocateRelationReadRepositoryImpl implements AllocateRelationReadRepository {

    @Resource
    AllocateRelationService allocateRelationService;

    @Override
    public PageResult<List<AllocateRelationSDO>> queryPageList(AllocateRelationSDO allocateRelationSDO) {
        PageResult<List<AllocateRelationSDO>> pageResult = new PageResult<>();
        AllocateRelationLoadListRequest allocateRelationLoadListRequest = new AllocateRelationLoadListRequest();
        if (StringUtils.isNotBlank(allocateRelationSDO.getOrganizationCode())){
            allocateRelationLoadListRequest.setOrganizationCode(allocateRelationSDO.getOrganizationCode());
        }
        if (CollectionUtils.isNotEmpty(allocateRelationSDO.getOrganizationCodes())){
            allocateRelationLoadListRequest.setOrganizationCodes(allocateRelationSDO.getOrganizationCodes());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getSaleChannelCode())){
            allocateRelationLoadListRequest.setSaleChannelCode(allocateRelationSDO.getSaleChannelCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getDepartmentCode())){
            allocateRelationLoadListRequest.setDepartmentCode(allocateRelationSDO.getDepartmentCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getCustomerCode())){
            allocateRelationLoadListRequest.setCustomerCode(allocateRelationSDO.getCustomerCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getWareHouseCode())){
            allocateRelationLoadListRequest.setWareHouseCode(allocateRelationSDO.getWareHouseCode());
        }
        if (StringUtils.isNotEmpty(allocateRelationSDO.getReceiveWareHouseCode())){
            allocateRelationLoadListRequest.setReceiveWareHouseCode(allocateRelationSDO.getReceiveWareHouseCode());
        }
        if (Objects.nonNull(allocateRelationSDO.getIsEnable())){
            allocateRelationLoadListRequest.setIsEnable(allocateRelationSDO.getIsEnable());
        }
        if (null!=allocateRelationSDO.getStart()){
            allocateRelationLoadListRequest.setStart(allocateRelationSDO.getStart());
        }
        if (null!=allocateRelationSDO.getLimit()){
            allocateRelationLoadListRequest.setLimit(allocateRelationSDO.getLimit());
        }
        if (null!=allocateRelationSDO.getId()){
            allocateRelationLoadListRequest.setId(Long.parseLong(allocateRelationSDO.getId()));
        }
        if (null!=allocateRelationSDO.getAddressCode()){
            allocateRelationLoadListRequest.setAddressCode(allocateRelationSDO.getAddressCode());
        }
        if (null!=allocateRelationSDO.getAddressId()){
            allocateRelationLoadListRequest.setAddressId(allocateRelationSDO.getAddressId());
        }
        if (null!=allocateRelationSDO.getAddressName()){
            allocateRelationLoadListRequest.setAddressName(allocateRelationSDO.getAddressName());
        }
        Result<List<AllocateRelation>> listResult = allocateRelationService.loadAllocateRelationList(allocateRelationLoadListRequest);
        if (listResult==null || CollectionUtils.isEmpty(listResult.getResult())){
            return pageResult;
        }
        List<AllocateRelation> allocateRelationList = listResult.getResult();
        List<AllocateRelationSDO> allocateRelationSDOList =allocateRelationList.stream().map(allocateRelation -> {
            AllocateRelationSDO relationSDO = new AllocateRelationSDO();
            relationSDO.setId(allocateRelation.getId().toString());
            relationSDO.setCustomerCode(allocateRelation.getCustomerCode());
            relationSDO.setCustomerName(allocateRelation.getCustomerName());
            relationSDO.setOrganizationCode(allocateRelation.getOrganizationCode());
            relationSDO.setOrganizationName(allocateRelation.getOrganizationName());
            relationSDO.setOrganizationOutCode(allocateRelation.getOrganizationOutCode());
            relationSDO.setSaleChannelCode(allocateRelation.getSaleChannelCode());
            relationSDO.setSaleChannelName(allocateRelation.getSaleChannelName());
            relationSDO.setSaleChannelOutCode(allocateRelation.getSaleChannelOutCode());
            relationSDO.setDepartmentCode(allocateRelation.getDepartmentCode());
            relationSDO.setDepartmentName(allocateRelation.getDepartmentName());
            relationSDO.setDepartmentOutCode(allocateRelation.getDepartmentOutCode());
            relationSDO.setWareHouseCode(allocateRelation.getWareHouseCode());
            relationSDO.setWareHouseName(allocateRelation.getWareHouseName());
            relationSDO.setReceiveWareHouseCode(allocateRelation.getReceiveWareHouseCode());
            relationSDO.setReceiveWareHouseName(allocateRelation.getReceiveWareHouseName());
            relationSDO.setRemark(allocateRelation.getRemark());
            relationSDO.setIsEnable(allocateRelation.getIsEnable());
            relationSDO.setIsCheckInventory(allocateRelation.getIsCheckInventory());
            relationSDO.setIsStrategyFirst(allocateRelation.getIsStrategyFirst());
            relationSDO.setAddressId(allocateRelation.getAddressId());
            relationSDO.setAddressCode(allocateRelation.getAddressCode());
            relationSDO.setAddressName(allocateRelation.getAddressName());
            return relationSDO;
        }).collect(Collectors.toList());
        //根据修改时间进行排序
        allocateRelationSDOList.stream().sorted(Comparator.comparing(AllocateRelationSDO::getGmtCreate).reversed());
        Collections.reverse(allocateRelationSDOList);
        pageResult.setContent(allocateRelationSDOList);
//        pageResult.setTotal(allocateRelationSDOList.size());
        pageResult.setTotal(listResult.getTotal());
        return pageResult;
    }

    public Result<Boolean> checkExist(AllocateRelationSDO allocateRelationSDO){
        AllocateRelationLoadListVO allocateRelationLoadListVO=new AllocateRelationLoadListVO();
        allocateRelationLoadListVO.setAddressCode(allocateRelationSDO.getAddressCode());
        allocateRelationLoadListVO.setDepartmentCode(allocateRelationSDO.getDepartmentCode());
        allocateRelationLoadListVO.setSaleChannelCode(allocateRelationSDO.getSaleChannelCode());
        CheckExistResponse checkExistResponse = allocateRelationService.checkExist(allocateRelationLoadListVO);
        if (Objects.nonNull(checkExistResponse)&&Objects.nonNull(checkExistResponse.getTotal())&&checkExistResponse.getTotal()>0){
            return Result.success(true);
        }
        return Result.success(false);
    }
}
